# -*- coding: utf-8 -*-
"""
# --------------------------------------------------------
# @Author : Pan
# @E-mail : 
# @Date   : 2025-10-29 16:52:22
# @Brief  :
# --------------------------------------------------------
"""
import os
import numpy as np
from typing import List, Dict


class InferBase:
    def __init__(self, llm_model: Dict, **kwargs) -> None:
        self.llm_model = llm_model
        self.kwargs = kwargs

    def chat(self, messages: Dict, stream=False, thinking=False, **kwargs):
        """
        通用对话功能
        :param messages:
        :param stream:
        :param thinking:
        :param kwargs:
        :return:
        """
        pass

    def chat_image(self, text: str, image: np.ndarray | str = None, thinking=False, stream=False, **kwargs):
        """
        :param text:
        :param image:
        :param stream:
        :param kwargs:
        :return:
        """
        pass

    def chat_video(self, text: str, frames: List = [], thinking=False, stream=False, **kwargs):
        """
        视频帧序列实时分析功能
        :param text:  用户问题/任务描述
        :param frames: 视频帧序列，RGB图像
        :param thinking:
        :param stream:
        :param kwargs:
        :return:
        """
        pass

    def chat_video_file(self, text: str, file: str = None, freq=2, stream=False, thinking=False, **kwargs):
        """
        视频文件实时分析功能
        :param text:  用户问题/任务描述
        :param file: 视频文件路径
        :param freq: 视频帧提取频率（Hz）
        :param stream:
        :param thinking:
        :param kwargs:
        :return:
        """
        pass

    def camera_example(self, text, file, freq=2, winsize=10, overlap=0.5, stream=False, thinking=False):
        """
        实时分析功能，从视频中提取帧进行分析
        :param text: 用户问题/任务描述
        :param file: 视频文件路径
        :param freq: 视频帧提取频率（Hz）
        :param winsize: 滑动窗口处理
        :param overlap: 滑动窗口重叠比例
        :param thinking: 启用思维链推理，默认False
        :param stream: 是否启用流式输出
        :return:
        """
        from pybaseutils.cvutils import video_utils
        from pybaseutils.base import list_queue
        w, h, nums, fps = video_utils.get_video_info(file)
        video_cap = video_utils.video_iterator(file, freq=freq, vis=False)
        queue = list_queue.Queue(maxsize=100)
        results = []
        for data_info in video_cap:
            # TODO {"count": count, "time": t, "frame": frame, "w": w, "h": h, "fps": fps, "offset": count}
            queue.put(data_info, block=False)
            while queue.qsize() > winsize:
                data_list = queue.get_window(winsize=winsize, overlap=overlap)  # 提取窗口数据
                times_ = [data_list[0]['offset'], data_list[-1]['offset']]
                count = [info['count'] for info in data_list]
                frames = [data['frame'] for data in data_list]
                result = self.chat_video(text, frames=frames, thinking=thinking, stream=stream)
                results.append({"time": times_, "count": count, "result": result})
                print("分析结果: time={:20s},offset={}, result={}".format(str(times_), count, result))
        return results


if __name__ == '__main__':
    from app.config import config

    llm_names = "Qwen3-VL-2B-Instruct"
    llm_model = config.llm_model_zoos[llm_names]
    image_file = "../../../data/image1.jpg"
    video_file = "../../../data/video2.mp4"
    infer = InferBase(llm_model)
    # 原有功能测试
    result = infer.chat_image("请描述这张图片", image=image_file, stream=False)
    # result = infer.chat_video("请描述这个视频", video=video_file, stream=False)
    # result = infer.camera_example("请描述视频中人正在做什么", video=video_file)
    print("摄像头分析结果:", result)
